import { replaceWservice } from "./common";
// vaProtocol   JGY  hxm   zhenguHref
const DefaultConfig ={
    CENTER_SOURCE:'b_zhengquan_test20',
}
  //重写埋点 不然会报错

  window.hxmClickStat = function (t, i) {
    console.log("Stat1=", t, i);
  };

  window.hxmJumpPageStat = function (a, b, c) {
    console.log("Stat2=", a, b, c);
  };
  window.hxmEventStat = function (a, b) {
    console.log("Stat3=", a, b);
  };
  window.hxmJumpNativeStat = function (a, b, c) {
    console.log("Stat4=", a, b, c);
  };
  window.hxmOnceStat = function (a, b) {
    console.log("Stat5=", a, b);
  };
  window.hxmPageStat = function (a, b) {
    console.log("Stat6=", a, b);
  };
  window.hxmStat = function (a) {
    console.log("Stat7=", a);
  };
     // 屏蔽埋点事件
     window.ClientMonitor = ()=>{}
     window.weblog = {
       report:()=>{},
       setConfig:()=>{}
     }
export function rewriteJGYFunc({callback,config}={}) {
    const initConfig={...DefaultConfig,...config};
    function handleUrlCallback(name,jumpUrl){
        if(callback && typeof callback=== "function"){
            const result= callback(`urlJumper.${name}`,jumpUrl) ;
            if(result){
             window.open(result);
            }else if(result === undefined){
              window.open(jumpUrl);
            }
         }else {
             window.open(jumpUrl);
         }
    }
    
  // 添加全局方法做兼容;
  //重新重新搜索方法
  window.vaProtocol = {
    protocol: function (v) {
      
      const question = v.split("sendMsg^query=")[1];
      const params = {
        question: question,
      };
      if(callback && typeof callback=== "function"){
        callback('vaProtocol.protocol',params);
      }
    },
  };

  // 重写查看更多跳转（问句：迈瑞医疗研报）
  // ai-stable-new.iwencai.com地址客户端直接访问获取不到数据，需要经过服务端转发一道
  window.JGY.showMoreClick = function (t, e) {
    console.log("showMoreClick",e);
    if(!t){
        return  false;
    }
    let url = replaceWservice(t);
    const jumpUrl = window.location.protocol + "//wservice.iwencai.com/test/html/wencaimobileresult/mactable.html?url=" + encodeURIComponent(encodeURIComponent(url))
    handleUrlCallback('JGY.showMoreClick',jumpUrl)
  };
  /**
   * 行情跳转
   * @param {*} code 股票代码
   * @param {*} marketId 市场ID
   * @param {*} domain 领域：abs_基金领域、abs_指数领域、abs_股票领域、abs_港股领域、abs_美股领域
   * @param {*} i 点击的行号
   * @param {*} fundType 基金类型（平安定制化）
   */
  window.JGY.JumpHangqing = function (code, marketId, domain, i, fundType) {
    console.log(code, marketId, domain, i, fundType);
    if(callback && typeof callback=== "function"){
        callback('JGY.JumpHangqing',{code,marketId,domain,i,fundType}) ;
    }
  };
  // 拦截analysis-show-more(问句：晶盛机电属于什么商业模式)
  window.JGY.clickTougudianpingMore = function (config) {
    console.log("clickTougudianpingMore--");
    window.JGY.jumpFun(config.url);
  };
  //重写咨讯列表跳转
  window.JGY.clickNewsList1Title = function (t) {
    const url = t.show_detail;
    window.JGY.jumpFun(url);
  };
  //重写a标签跳转
  window.JGY.ajumper = function (url) {
    console.log("ajumper--");
    handleUrlCallback('JGY.ajumper',url)
  };
  //重写诊股咨讯跳转
  window.JGY.clickList1Li = function (t) {
    console.log("clickList1Li--");
    handleUrlCallback('JGY.clickList1Li',t.url)
  };
  //重新表格描述跳转（问句：马云2，底部的网络印象点击不跳转）
  window.JGY.eventTableDescClick = function (rowIndex, index, url) {
    console.log(rowIndex, index, url);
    return;
  };
  // table2点击事件（问句：迈瑞医疗研报）
  // ai-stable-new.iwencai.com地址客户端直接访问获取不到数据，需要经过服务端转发一道
  window.JGY.clickTable2Detail = function (obj) {
    console.log("clickTable2Detail");
    if(obj && obj.footer_info && obj.footer_info.url){
        let url = replaceWservice(obj.footer_info.url);
        const jumpUrl = window.location.protocol + "//wservice.iwencai.com/test/html/wencaimobileresult/mactable.html?url=" + encodeURIComponent(encodeURIComponent(url))
        handleUrlCallback('JGY.clickTable2Detail',jumpUrl)

    }
  };
  window.JGY.jumpFun = function (url,data,domain) {
    console.log("---window.JGY.jumpFun---", url,data,domain);
    if(url.indexOf('client.html?action=')>-1){
        //行情跳转
        const code = data.hq_code;//建议从url里获取
        const marketId= data.market_hq_code //建议从url里获取
        let domainValue = domain
        if(!domainValue){
          if(this.configData.show_type === 'fundTxt'){
            const domains=this.configData.data.columns.filter(item=>item.domain);
            if(domains[0]){
              domainValue=domains[0].domain
            }
          }
        }
        console.log('domainValue',domainValue,code,marketId)
        if(callback && typeof callback=== "function"){
            callback('client',code,marketId,domainValue)
        }
        //jumphangqing
      }else{
        const jumpUrl = replaceWservice(url);
        handleUrlCallback('JGY.jumpFun',jumpUrl)
      }
    
    
  };

  window.getUserid = function () {
    console.log("getUserid 123");
    return "123";
  };
  window.JGY.title1Click = function (data,url) {
    if(data.h1 === '大事提醒'){
        return false;
    }else if(url){
        const jumpUrl =replaceWservice(url);
        handleUrlCallback('JGY.title1Click',jumpUrl);
    }
  };

  window.zhenguHref = {
    fenshiPage(hqcode, marketId, originCode) {
      console.log("fenshiPagefenshiPagefenshiPagefenshiPage");
      if(callback && typeof callback=== "function"){
        callback('zhenguHref.fenshiPage',{hqcode,marketId,originCode})
      }
    },
    iwencaiPage: function (query, domain) {
      //诊股更多跳b端
      let source = initConfig.CENTER_SOURCE,
        version = "2.0"
      const url =`https://ths8.iwencai.com/zxg/?showAd=false&source=${source}&querytype=${encodeURIComponent(domain)}&version=${version}#/result?question=${query}`
      handleUrlCallback('zhenguHref.iwencaiPage',url)
    },
  };
}
